package com.lu.pojo.thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author luzhenfang
 * @date 2020 05 23
 * @description 多线程下载图片
 */

// 练习thread 实现多线程下载
class TestDownload extends Thread{
    private String url;
    private String name;

    public TestDownload(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        Downloader dowoloader = new Downloader();
        dowoloader.download(url,name);
        System.out.println("下载文件名为:"+name);

    }
}
class Downloader {
    public void download(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,download方法出现问题！");
        }
    }

    public static void main(String[] args) {
        new TestDownload("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2846085407,1355312244&fm=26&gp=0.jpg","1.png").start();
        new TestDownload("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3699419459,1658025062&fm=26&gp=0.jpg","2.png").start();
        new TestDownload("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=435087364,3328369761&fm=26&gp=0.jpg","3.png").start();

    }
}
